package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.DateAnalysisMapper;
import com.youhome.service.model.*;
import com.youhome.service.response.DateValue;
import com.youhome.service.response.StatParam;
import com.youhome.service.service.BCompanyService;
import com.youhome.service.service.DateAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2018/1/2.
 */
@Service
public class DateAnalysisServiceImpl implements DateAnalysisService {
    @Autowired
    private DateAnalysisMapper dateAnalysisMapper;
    @Autowired
    private BCompanyService bCompanyService;

    @Override
    public List<DateValue> statSMSByDate(Principal principal, Date btime, Date etime) throws CustomException {

        if (StringUtils.isEmpty(btime)) {
            throw new CustomException(ResultCode.INVALID_BEGIN_DATE);
        }
        if (StringUtils.isEmpty(etime)) {
            throw new CustomException(ResultCode.INVALID_END_DATE);
        }

        Long userId = principal.getUser().getUserId();

        StatParam statParam = new StatParam();
        statParam.setBtime(btime);
        statParam.setEtime(etime);
        statParam.setUserId(userId);
        statParam.setStatus(1);

        return dateAnalysisMapper.statSMSByDateUserId(statParam);
    }

    @Override
    public List<DateValue> statMailByDate(Principal principal, Date btime, Date etime) throws CustomException {
        if (StringUtils.isEmpty(btime)) {
            throw new CustomException(ResultCode.INVALID_BEGIN_DATE);
        }
        if (StringUtils.isEmpty(etime)) {
            throw new CustomException(ResultCode.INVALID_END_DATE);
        }

        Long userId = principal.getUser().getUserId();

        StatParam statParam = new StatParam();
        statParam.setBtime(btime);
        statParam.setEtime(etime);
        statParam.setUserId(userId);
        statParam.setStatus(1);
        return dateAnalysisMapper.statMailByDateUserId(statParam);
    }

    @Override
    public List<DateValue> statLogByBean(String uuid, Date btime, Date etime, String classMethod) throws CustomException {
        if (StringUtils.isEmpty(uuid)) {
            throw new CustomException(ResultCode.INVALID_COMPANY_UUID);
        }
        if (StringUtils.isEmpty(btime)) {
            throw new CustomException(ResultCode.INVALID_BEGIN_DATE);
        }
        if (StringUtils.isEmpty(etime)) {
            throw new CustomException(ResultCode.INVALID_END_DATE);
        }

        BLog statParam = new BLog();
        statParam.setBtime(btime);
        statParam.setEtime(etime);
        statParam.setClassMethod(classMethod); //公司落地页详情，每查看一次相当访问一次
        statParam.setParams("uuid=" + uuid);
        return dateAnalysisMapper.statLogByBean(statParam);

    }

    @Override
    public List<DateValue> statClientRegisterCount(String uuid, Date btime, Date etime) throws CustomException {
        if (StringUtils.isEmpty(uuid)) {
            throw new CustomException(ResultCode.INVALID_COMPANY_UUID);
        }
        if (StringUtils.isEmpty(btime)) {
            throw new CustomException(ResultCode.INVALID_BEGIN_DATE);
        }
        if (StringUtils.isEmpty(etime)) {
            throw new CustomException(ResultCode.INVALID_END_DATE);
        }
        BCompany bCompany = bCompanyService.selectByUUID(uuid);
        if (bCompany == null) {
            throw new CustomException(ResultCode.NOT_EXIST_COMPANY);
        }
        BClient statParam = new BClient();
        statParam.setBtime(btime);
        statParam.setEtime(etime);
        statParam.setCompanyId(bCompany.getCompanyId());

        return dateAnalysisMapper.statClientRegisterCountByCompanyIdAndDate(statParam);

    }

    @Override
    public List<DateValue> statLogByBean(Principal principal,Date btime, Date etime) throws CustomException {
        if (StringUtils.isEmpty(btime)) {
            throw new CustomException(ResultCode.INVALID_BEGIN_DATE);
        }
        if (StringUtils.isEmpty(etime)) {
            throw new CustomException(ResultCode.INVALID_END_DATE);
        }
        Long userId = principal.getUser().getUserId();

        BLog statParam = new BLog();
        statParam.setBtime(btime);
        statParam.setEtime(etime);
        statParam.setUserId(userId);

        return dateAnalysisMapper.statLogByBean(statParam);

    }
}
